package com.rc.saas.tenant.service.tenant;

import com.rc.saas.tenant.mapper.tenant.TenantOpenServiceMapper;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenService;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenServiceExample;
import com.rc.saas.tenant.model.tenant.TenantOpenService;
import com.rc.saas.tenant.model.tenant.TenantOpenServiceExample;
import com.rc.saas.tenant.service.operator.TenantOperatorOpenServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TenantOpenServiceServiceImpl implements TenantOpenServiceService {
    @Autowired
    private TenantOpenServiceMapper tenantOpenServiceMapper;

    @Autowired
    private TenantOperatorOpenServiceService tenantOperatorOpenServiceService;

    @Override
    public TenantOpenServiceMapper getMapper() {
        return tenantOpenServiceMapper;
    }

    @Override
    public TenantOpenService selectByTypeCode(String tenantCode, String serviceTypeCode) {
        TenantOpenServiceExample example = new TenantOpenServiceExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andServiceTypeCodeEqualTo(serviceTypeCode);
        List<TenantOpenService> tenantOpenServices = tenantOpenServiceMapper.selectByExample(example);
        if (tenantOpenServices != null && tenantOpenServices.size() > 0) {
            return tenantOpenServices.get(0);
        }
        return null;
    }

    @Override
    public void updateTenantAndOperator(TenantOpenService tenantOpenService) {
        TenantOpenServiceExample tenantOpenServiceExample = new TenantOpenServiceExample();
        tenantOpenServiceExample.createCriteria().andServiceTypeCodeEqualTo(tenantOpenService.getServiceTypeCode()).andTenantCodeEqualTo(tenantOpenService.getTenantCode());
        tenantOpenServiceMapper.updateByExampleSelective(tenantOpenService, tenantOpenServiceExample);

        if ("OPERATORACCT".equals(tenantOpenService.getServiceTypeCode())) {

            TenantOperatorOpenServiceExample example = new TenantOperatorOpenServiceExample();
            example.createCriteria().andTenantCodeEqualTo(tenantOpenService.getTenantCode()).andServiceTypeCodeEqualTo(tenantOpenService.getServiceTypeCode());

            TenantOperatorOpenService tenantOperatorOpenService = new TenantOperatorOpenService();
            tenantOperatorOpenService.setStatus(tenantOpenService.getStatus());
            tenantOperatorOpenServiceService.updateByExampleSelective(tenantOperatorOpenService, example);
        }
    }
}
